
/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.tompai.datagather.twitter;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tompai.common.http.HttpClientUtil;
import com.tompai.common.http.HttpClientUtilThread;
import com.tompai.datagather.utils.TwitterParamater;


/**
 * @desc: datagather-tools
 * @name: ShadowSockets5Proxy.java
 * @author: tompai
 * @createTime: 2018年5月3日 下午4:29:23
 * @history:
 * @version: v1.0
 */

public class ShadowSockets5Proxy {

	public static final Logger LOG = LoggerFactory.getLogger(ShadowSockets5Proxy.class);

	public static String twitterLink(String url) {
		String result = null;
		int index = 0;
		try {
			// String url="https://twitter.com";
			// String url="https://twitter.com/i/search/timeline?q=happy&f=tweets";
			String twitterCookie = TwitterParamater.getCookie();
			Map<String, String> headers = new HashMap<String, String>();
			// headers.put(":authority", "twitter.com");
			// headers.put(":method", "GET");
			// headers.put(":scheme", "https");
			headers.put("accept", "pplication/json, text/javascript, */*; q=0.01");
			headers.put("accept-Encoding", "gzip, deflate, br");
			// 在请求中明确定义不要进行压缩
			// headers.put("Accept-Encoding", "identity"); 
			headers.put("accept-Language", "zh-CN,zh;q=0.9");
			headers.put("cookie", twitterCookie);
			headers.put("referer", url);
			headers.put("user-Agent",
					"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
			headers.put("x-asset-version", "277db2");
			headers.put("x-push-state-request", "true");
			headers.put("x-requested-with", "XMLHttpRequest");
			headers.put("x-twitter-active-user", "yes");
			do {
				index++;
				//LOG.info("try to use the proxy times: "+index);
				// result = HttpClientUtil.getWithProxy(url, headers, "UTF-8");   
				result =HttpClientUtilThread.getWithProxy(url, headers, "UTF-8");   
			} while (StringUtils.isEmpty(result) && index < TwitterParamater.PROXY_MAX_RESPONSE);

			if(StringUtils.isEmpty(result)||index>=TwitterParamater.PROXY_MAX_RESPONSE) {
				 LOG.error("Proxy failed:"+url);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String facebookLink(String url) {

		return null;
		// will do something later...
	}
}
